package com.own.business.log.login.persistence.business.hook;

import com.own.business.log.login.persistence.business.service.LogLoginElasticService;
import com.own.component.log.login.core.BaseLastLoginTimeMethod;
import com.own.component.log.login.em.LogLoginStatusEnum;
import com.own.component.log.login.entity.LogLoginEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * DefaultLastLoginTimeMethod
 *
 * @author chenxueli
 * @date 2024-03-13 09:40:00
 */
@Slf4j
@Component("baseLastLoginTimeMethod")
public class DefaultLastLoginTimeMethod implements BaseLastLoginTimeMethod {

    @Resource
    private LogLoginElasticService logLoginElasticService;

    /**
     * 根据用户id查询最后一次成功的登录日志信息
     *
     * @param userId 用户id
     * @return 最后一次成功的登录日志信息
     */
    @Override
    public LogLoginEntity lastLoginTime(Long userId) {
        var logLogin = logLoginElasticService.getLastLoginTimeByUserId(userId);
        // 转成entity
        if (logLogin != null) {
            return new LogLoginEntity()
                    .setStatus(LogLoginStatusEnum.SUCCESS)
                    .setLoginAccount(logLogin.getLoginAccount())
                    .setLoginType(logLogin.getLoginType())
                    .setLoginCode(logLogin.getLoginCode())
                    .setIp(logLogin.getIp())
                    .setClient(logLogin.getClient())
                    .setLoginTime(logLogin.getGmtCreate());
        }
        return LogLoginEntity.NONE;
    }


}
